*******************************
****					   ****
****     FATA PROJECT      ****
**** (dataset preparation) ****
****					   ****
*******************************


global PathData = "~/Dropbox/FATA_fractionalization/1_Data/"
global PathRes = "~/Dropbox/FATA_fractionalization/4_Results/"

use "${PathData}FATA_Data_geocoded.dta", clear
drop in 2799
merge m:1 tribe using "${PathData}tribes_detailed.dta", nogenerate 


replace tribe_code=9 if tribe_code==48 
replace tribe_code=14 if tribe_code==50 
replace tribe_code=20 if tribe_code==24
replace tribe_code=21 if tribe_code==61
replace tribe_code=30 if tribe_code==59
replace tribe_code=38 if tribe_code==69
replace tribe_code=4 if tribe_code==42

replace tribe_name="Burki" if tribe_name=="Burki "
replace tribe_name="Muqbal" if tribe_name=="Muqbil"
replace tribe_name="Wazir" if tribe_name=="Wazir "

egen tribe_rel=concat(tribe religion)
tab tribe_rel, gen(tribe)



******************************
****     VILLAGE FRAC     ****
******************************

rename vill_code vill_code_wrong
egen vill_code=concat(vill_code  tehs_code)

bys vill_code: egen p_tribe1 = mean(tribe1) 
bys vill_code: egen p_tribe2 = mean(tribe2) 
bys vill_code: egen p_tribe3 = mean(tribe3) 
bys vill_code: egen p_tribe4 = mean(tribe4) 
bys vill_code: egen p_tribe5 = mean(tribe5) 
bys vill_code: egen p_tribe6 = mean(tribe6) 
bys vill_code: egen p_tribe7 = mean(tribe7) 
bys vill_code: egen p_tribe8 = mean(tribe8) 
bys vill_code: egen p_tribe9 = mean(tribe9) 
bys vill_code: egen p_tribe10 = mean(tribe10)
bys vill_code: egen p_tribe11 = mean(tribe11)
bys vill_code: egen p_tribe12 = mean(tribe12)
bys vill_code: egen p_tribe13 = mean(tribe13)
bys vill_code: egen p_tribe14 = mean(tribe14)
bys vill_code: egen p_tribe15 = mean(tribe15)
bys vill_code: egen p_tribe16 = mean(tribe16)
bys vill_code: egen p_tribe17 = mean(tribe17)
bys vill_code: egen p_tribe18 = mean(tribe18)
bys vill_code: egen p_tribe19 = mean(tribe19)
bys vill_code: egen p_tribe20 = mean(tribe20)
bys vill_code: egen p_tribe21 = mean(tribe21)
bys vill_code: egen p_tribe22 = mean(tribe22)
bys vill_code: egen p_tribe23 = mean(tribe23)
bys vill_code: egen p_tribe24 = mean(tribe24)
bys vill_code: egen p_tribe25 = mean(tribe25)
bys vill_code: egen p_tribe26 = mean(tribe26)
bys vill_code: egen p_tribe27 = mean(tribe27)
bys vill_code: egen p_tribe28 = mean(tribe28)
bys vill_code: egen p_tribe29 = mean(tribe29)
bys vill_code: egen p_tribe30 = mean(tribe30)
bys vill_code: egen p_tribe31 = mean(tribe31)
bys vill_code: egen p_tribe32 = mean(tribe32)
bys vill_code: egen p_tribe33 = mean(tribe33)
bys vill_code: egen p_tribe34 = mean(tribe34)
bys vill_code: egen p_tribe35 = mean(tribe35)
bys vill_code: egen p_tribe36 = mean(tribe36)
bys vill_code: egen p_tribe37 = mean(tribe37)
bys vill_code: egen p_tribe38 = mean(tribe38)
bys vill_code: egen p_tribe39 = mean(tribe39)
bys vill_code: egen p_tribe40 = mean(tribe40)
bys vill_code: egen p_tribe41 = mean(tribe41)
bys vill_code: egen p_tribe42 = mean(tribe42)
bys vill_code: egen p_tribe43 = mean(tribe43)
bys vill_code: egen p_tribe44 = mean(tribe44)
bys vill_code: egen p_tribe45 = mean(tribe45)
bys vill_code: egen p_tribe46 = mean(tribe46)
bys vill_code: egen p_tribe47 = mean(tribe47)
bys vill_code: egen p_tribe48 = mean(tribe48)
bys vill_code: egen p_tribe49 = mean(tribe49)
bys vill_code: egen p_tribe50 = mean(tribe50)
bys vill_code: egen p_tribe51 = mean(tribe51)
bys vill_code: egen p_tribe52 = mean(tribe52)
bys vill_code: egen p_tribe53 = mean(tribe53)
bys vill_code: egen p_tribe54 = mean(tribe54)
bys vill_code: egen p_tribe55 = mean(tribe55)
bys vill_code: egen p_tribe56 = mean(tribe56)
bys vill_code: egen p_tribe57 = mean(tribe57)
bys vill_code: egen p_tribe58 = mean(tribe58)
bys vill_code: egen p_tribe59 = mean(tribe59)
bys vill_code: egen p_tribe60 = mean(tribe60)
bys vill_code: egen p_tribe61 = mean(tribe61)
bys vill_code: egen p_tribe62 = mean(tribe62)
bys vill_code: egen p_tribe63 = mean(tribe63)
bys vill_code: egen p_tribe64 = mean(tribe64)
bys vill_code: egen p_tribe65 = mean(tribe65)
bys vill_code: egen p_tribe66 = mean(tribe66)
bys vill_code: egen p_tribe67 = mean(tribe67)
bys vill_code: egen p_tribe68 = mean(tribe68)
bys vill_code: egen p_tribe69 = mean(tribe69)
bys vill_code: egen p_tribe70 = mean(tribe70)
bys vill_code: egen p_tribe71 = mean(tribe71)
bys vill_code: egen p_tribe72 = mean(tribe72)
bys vill_code: egen p_tribe73 = mean(tribe73)
bys vill_code: egen p_tribe74 = mean(tribe74)
bys vill_code: egen p_tribe75 = mean(tribe75)
bys vill_code: egen p_tribe76 = mean(tribe76)
bys vill_code: egen p_tribe77 = mean(tribe77)
bys vill_code: egen p_tribe78 = mean(tribe78)
bys vill_code: egen p_tribe79 = mean(tribe79)
bys vill_code: egen p_tribe80 = mean(tribe80)
bys vill_code: egen p_tribe81 = mean(tribe81)
bys vill_code: egen p_tribe82 = mean(tribe82)
bys vill_code: egen p_tribe83 = mean(tribe83)
bys vill_code: egen p_tribe84 = mean(tribe84)
bys vill_code: egen p_tribe85 = mean(tribe85)
bys vill_code: egen p_tribe86 = mean(tribe86)
bys vill_code: egen p_tribe87 = mean(tribe87)



bys vill_code: gen biggest_tribe_vill = max(p_tribe1, p_tribe2, p_tribe3, p_tribe4, p_tribe5, p_tribe6, p_tribe7, p_tribe8, p_tribe9, p_tribe10, p_tribe11, p_tribe12, p_tribe13, p_tribe14, p_tribe15, p_tribe16, p_tribe17, p_tribe18, p_tribe19, p_tribe20, p_tribe21, p_tribe22, p_tribe23, p_tribe24, p_tribe25, p_tribe26, p_tribe27, p_tribe28, p_tribe29, p_tribe30, p_tribe31, p_tribe32, p_tribe33, p_tribe34, p_tribe35, p_tribe36, p_tribe37, p_tribe38, p_tribe39, p_tribe40, p_tribe41, p_tribe42, p_tribe43, p_tribe44, p_tribe45, p_tribe46, p_tribe47, p_tribe48, p_tribe49, p_tribe50, p_tribe51, p_tribe52, p_tribe53, p_tribe54, p_tribe55, p_tribe56, p_tribe57, p_tribe58, p_tribe59, p_tribe60, p_tribe61, p_tribe62, p_tribe63, p_tribe64, p_tribe65, p_tribe66, p_tribe67, p_tribe68, p_tribe69, p_tribe70, p_tribe71, p_tribe72, p_tribe73, p_tribe74, p_tribe75, p_tribe76, p_tribe77, p_tribe78, p_tribe79, p_tribe80, p_tribe81, p_tribe82, p_tribe83, p_tribe84, p_tribe85, p_tribe86, p_tribe87)

gen max_tribe_vill=1 if p_tribe1==biggest_tribe_vill 
replace max_tribe_vill=2 if p_tribe2==biggest_tribe_vill 
replace max_tribe_vill=3 if p_tribe3==biggest_tribe_vill 
replace max_tribe_vill=4 if p_tribe4==biggest_tribe_vill 
replace max_tribe_vill=5 if p_tribe5==biggest_tribe_vill 
replace max_tribe_vill=6 if p_tribe6==biggest_tribe_vill 
replace max_tribe_vill=7 if p_tribe7==biggest_tribe_vill 
replace max_tribe_vill=8 if p_tribe8==biggest_tribe_vill 
replace max_tribe_vill=9 if p_tribe9==biggest_tribe_vill 
replace max_tribe_vill=10 if p_tribe10==biggest_tribe_vill 
replace max_tribe_vill=11 if p_tribe11==biggest_tribe_vill 
replace max_tribe_vill=12 if p_tribe12==biggest_tribe_vill 
replace max_tribe_vill=13 if p_tribe13==biggest_tribe_vill 
replace max_tribe_vill=14 if p_tribe14==biggest_tribe_vill 
replace max_tribe_vill=15 if p_tribe15==biggest_tribe_vill 
replace max_tribe_vill=16 if p_tribe16==biggest_tribe_vill 
replace max_tribe_vill=17 if p_tribe17==biggest_tribe_vill 
replace max_tribe_vill=18 if p_tribe18==biggest_tribe_vill 
replace max_tribe_vill=19 if p_tribe19==biggest_tribe_vill 
replace max_tribe_vill=20 if p_tribe20==biggest_tribe_vill 
replace max_tribe_vill=21 if p_tribe21==biggest_tribe_vill 
replace max_tribe_vill=22 if p_tribe22==biggest_tribe_vill 
replace max_tribe_vill=23 if p_tribe23==biggest_tribe_vill 
replace max_tribe_vill=24 if p_tribe24==biggest_tribe_vill 
replace max_tribe_vill=25 if p_tribe25==biggest_tribe_vill 
replace max_tribe_vill=26 if p_tribe26==biggest_tribe_vill 
replace max_tribe_vill=27 if p_tribe27==biggest_tribe_vill 
replace max_tribe_vill=28 if p_tribe28==biggest_tribe_vill 
replace max_tribe_vill=29 if p_tribe29==biggest_tribe_vill 
replace max_tribe_vill=30 if p_tribe30==biggest_tribe_vill 
replace max_tribe_vill=31 if p_tribe31==biggest_tribe_vill 
replace max_tribe_vill=32 if p_tribe32==biggest_tribe_vill 
replace max_tribe_vill=33 if p_tribe33==biggest_tribe_vill 
replace max_tribe_vill=34 if p_tribe34==biggest_tribe_vill 
replace max_tribe_vill=35 if p_tribe35==biggest_tribe_vill 
replace max_tribe_vill=36 if p_tribe36==biggest_tribe_vill 
replace max_tribe_vill=37 if p_tribe37==biggest_tribe_vill 
replace max_tribe_vill=38 if p_tribe38==biggest_tribe_vill 
replace max_tribe_vill=39 if p_tribe39==biggest_tribe_vill 
replace max_tribe_vill=40 if p_tribe40==biggest_tribe_vill 
replace max_tribe_vill=41 if p_tribe41==biggest_tribe_vill 
replace max_tribe_vill=42 if p_tribe42==biggest_tribe_vill 
replace max_tribe_vill=43 if p_tribe43==biggest_tribe_vill 
replace max_tribe_vill=44 if p_tribe44==biggest_tribe_vill 
replace max_tribe_vill=45 if p_tribe45==biggest_tribe_vill 
replace max_tribe_vill=46 if p_tribe46==biggest_tribe_vill 
replace max_tribe_vill=47 if p_tribe47==biggest_tribe_vill 
replace max_tribe_vill=48 if p_tribe48==biggest_tribe_vill 
replace max_tribe_vill=49 if p_tribe49==biggest_tribe_vill 
replace max_tribe_vill=50 if p_tribe50==biggest_tribe_vill 
replace max_tribe_vill=51 if p_tribe51==biggest_tribe_vill 
replace max_tribe_vill=52 if p_tribe52==biggest_tribe_vill 
replace max_tribe_vill=53 if p_tribe53==biggest_tribe_vill 
replace max_tribe_vill=54 if p_tribe54==biggest_tribe_vill 
replace max_tribe_vill=55 if p_tribe55==biggest_tribe_vill 
replace max_tribe_vill=56 if p_tribe56==biggest_tribe_vill 
replace max_tribe_vill=57 if p_tribe57==biggest_tribe_vill 
replace max_tribe_vill=58 if p_tribe58==biggest_tribe_vill 
replace max_tribe_vill=59 if p_tribe59==biggest_tribe_vill 
replace max_tribe_vill=60 if p_tribe60==biggest_tribe_vill 
replace max_tribe_vill=61 if p_tribe61==biggest_tribe_vill 
replace max_tribe_vill=62 if p_tribe62==biggest_tribe_vill 
replace max_tribe_vill=63 if p_tribe63==biggest_tribe_vill 
replace max_tribe_vill=64 if p_tribe64==biggest_tribe_vill 
replace max_tribe_vill=65 if p_tribe65==biggest_tribe_vill 
replace max_tribe_vill=66 if p_tribe66==biggest_tribe_vill 
replace max_tribe_vill=67 if p_tribe67==biggest_tribe_vill 
replace max_tribe_vill=68 if p_tribe68==biggest_tribe_vill 
replace max_tribe_vill=69 if p_tribe69==biggest_tribe_vill 
replace max_tribe_vill=70 if p_tribe70==biggest_tribe_vill 
replace max_tribe_vill=71 if p_tribe71==biggest_tribe_vill 
replace max_tribe_vill=72 if p_tribe72==biggest_tribe_vill 
replace max_tribe_vill=73 if p_tribe73==biggest_tribe_vill 
replace max_tribe_vill=74 if p_tribe74==biggest_tribe_vill 
replace max_tribe_vill=75 if p_tribe75==biggest_tribe_vill 
replace max_tribe_vill=76 if p_tribe76==biggest_tribe_vill 
replace max_tribe_vill=77 if p_tribe77==biggest_tribe_vill 
replace max_tribe_vill=78 if p_tribe78==biggest_tribe_vill 
replace max_tribe_vill=79 if p_tribe79==biggest_tribe_vill 
replace max_tribe_vill=80 if p_tribe80==biggest_tribe_vill 
replace max_tribe_vill=81 if p_tribe81==biggest_tribe_vill 
replace max_tribe_vill=82 if p_tribe82==biggest_tribe_vill 
replace max_tribe_vill=83 if p_tribe83==biggest_tribe_vill 
replace max_tribe_vill=84 if p_tribe84==biggest_tribe_vill 
replace max_tribe_vill=85 if p_tribe85==biggest_tribe_vill 
replace max_tribe_vill=86 if p_tribe86==biggest_tribe_vill 
replace max_tribe_vill=87 if p_tribe87==biggest_tribe_vill 


encode tribe_rel, gen(tribe_code2)

gen minority_vill = 1 
replace minority_vill = 0 if max_tribe_vill==tribe_code2 




foreach var of varlist p_tribe1 p_tribe2 p_tribe3 p_tribe4 p_tribe5 p_tribe6 p_tribe7 p_tribe8 p_tribe9 p_tribe10 p_tribe11 p_tribe12 p_tribe13 p_tribe14 p_tribe15 p_tribe16 p_tribe17 p_tribe18 p_tribe19 p_tribe20 p_tribe21 p_tribe22 p_tribe23 p_tribe24 p_tribe25 p_tribe26 p_tribe27 p_tribe28 p_tribe29 p_tribe30 p_tribe31 p_tribe32 p_tribe33 p_tribe34 p_tribe35 p_tribe36 p_tribe37 p_tribe38 p_tribe39 p_tribe40 p_tribe41 p_tribe42 p_tribe43 p_tribe44 p_tribe45 p_tribe46 p_tribe47 p_tribe48 p_tribe49 p_tribe50 p_tribe51 p_tribe52 p_tribe53 p_tribe54 p_tribe55 p_tribe56 p_tribe57 p_tribe58 p_tribe59 p_tribe60 p_tribe61 p_tribe62 p_tribe63 p_tribe64 p_tribe65 p_tribe66 p_tribe67 p_tribe68 p_tribe69 p_tribe70 p_tribe71 p_tribe72 p_tribe73 p_tribe74 p_tribe75 p_tribe76 p_tribe77 p_tribe78 p_tribe79 p_tribe80 p_tribe81 p_tribe82 p_tribe83 p_tribe84 p_tribe85 p_tribe86 p_tribe87{
gen p`var' = `var'*100
}

* Fractionalization index (VILLAGE)
foreach var of varlist pp_tribe1 pp_tribe2 pp_tribe3 pp_tribe4 pp_tribe5 pp_tribe6 pp_tribe7 pp_tribe8 pp_tribe9 pp_tribe10 pp_tribe11 pp_tribe12 pp_tribe13 pp_tribe14 pp_tribe15 pp_tribe16 pp_tribe17 pp_tribe18 pp_tribe19 pp_tribe20 pp_tribe21 pp_tribe22 pp_tribe23 pp_tribe24 pp_tribe25 pp_tribe26 pp_tribe27 pp_tribe28 pp_tribe29 pp_tribe30 pp_tribe31 pp_tribe32 pp_tribe33 pp_tribe34 pp_tribe35 pp_tribe36 pp_tribe37 pp_tribe38 pp_tribe39 pp_tribe40 pp_tribe41 pp_tribe42 pp_tribe43 pp_tribe44 pp_tribe45 pp_tribe46 pp_tribe47 pp_tribe48 pp_tribe49 pp_tribe50 pp_tribe51 pp_tribe52 pp_tribe53 pp_tribe54 pp_tribe55 pp_tribe56 pp_tribe57 pp_tribe58 pp_tribe59 pp_tribe60 pp_tribe61 pp_tribe62 pp_tribe63 pp_tribe64 pp_tribe65 pp_tribe66 pp_tribe67 pp_tribe68 pp_tribe69 pp_tribe70 pp_tribe71 pp_tribe72 pp_tribe73 pp_tribe74 pp_tribe75 pp_tribe76 pp_tribe77 pp_tribe78 pp_tribe79 pp_tribe80 pp_tribe81 pp_tribe82 pp_tribe83 pp_tribe84 pp_tribe85 pp_tribe86 pp_tribe87{
gen one_minus_`var' = 100-`var'
gen multipl_`var' = `var'*one_minus_`var'
}
egen temp_tribal_frac=rowtotal(multipl_*), missing
gen tribal_frac0 = temp_tribal_frac/10000
drop temp_* one_minus* multipl_*
label variable tribal_frac0 "Tribal fractionalization (village)"


drop p_tribe1 p_tribe2 p_tribe3 p_tribe4 p_tribe5 p_tribe6 p_tribe7 p_tribe8 p_tribe9 p_tribe10 p_tribe11 p_tribe12 p_tribe13 p_tribe14 p_tribe15 p_tribe16 p_tribe17 p_tribe18 p_tribe19 p_tribe20 p_tribe21 p_tribe22 p_tribe23 p_tribe24 p_tribe25 p_tribe26 p_tribe27 p_tribe28 p_tribe29 p_tribe30 p_tribe31 p_tribe32 p_tribe33 p_tribe34 p_tribe35 p_tribe36 p_tribe37 p_tribe38 p_tribe39 p_tribe40 p_tribe41 p_tribe42 p_tribe43 p_tribe44 p_tribe45 p_tribe46 p_tribe47 p_tribe48 p_tribe49 p_tribe50 p_tribe51 p_tribe52 p_tribe53 p_tribe54 p_tribe55 p_tribe56 p_tribe57 p_tribe58 p_tribe59 p_tribe60 p_tribe61 p_tribe62 p_tribe63 p_tribe64 p_tribe65 p_tribe66 p_tribe67 p_tribe68 p_tribe69 p_tribe70 p_tribe71 p_tribe72 p_tribe73 p_tribe74 p_tribe75 p_tribe76 p_tribe77 p_tribe78 p_tribe79 p_tribe80 p_tribe81 p_tribe82 p_tribe83 p_tribe84 p_tribe85 p_tribe86 p_tribe87 pp_tribe1 pp_tribe2 pp_tribe3 pp_tribe4 pp_tribe5 pp_tribe6 pp_tribe7 pp_tribe8 pp_tribe9 pp_tribe10 pp_tribe11 pp_tribe12 pp_tribe13 pp_tribe14 pp_tribe15 pp_tribe16 pp_tribe17 pp_tribe18 pp_tribe19 pp_tribe20 pp_tribe21 pp_tribe22 pp_tribe23 pp_tribe24 pp_tribe25 pp_tribe26 pp_tribe27 pp_tribe28 pp_tribe29 pp_tribe30 pp_tribe31 pp_tribe32 pp_tribe33 pp_tribe34 pp_tribe35 pp_tribe36 pp_tribe37 pp_tribe38 pp_tribe39 pp_tribe40 pp_tribe41 pp_tribe42 pp_tribe43 pp_tribe44 pp_tribe45 pp_tribe46 pp_tribe47 pp_tribe48 pp_tribe49 pp_tribe50 pp_tribe51 pp_tribe52 pp_tribe53 pp_tribe54 pp_tribe55 pp_tribe56 pp_tribe57 pp_tribe58 pp_tribe59 pp_tribe60 pp_tribe61 pp_tribe62 pp_tribe63 pp_tribe64 pp_tribe65 pp_tribe66 pp_tribe67 pp_tribe68 pp_tribe69 pp_tribe70 pp_tribe71 pp_tribe72 pp_tribe73 pp_tribe74 pp_tribe75 pp_tribe76 pp_tribe77 pp_tribe78 pp_tribe79 pp_tribe80 pp_tribe81 pp_tribe82 pp_tribe83 pp_tribe84 pp_tribe85 pp_tribe86 pp_tribe87

bys vill_code: gen village=_n



******************************
****     TEHSIL FRAC      ****
******************************


bys tehs_code: egen p_tribe1 = mean(tribe1) 
bys tehs_code: egen p_tribe2 = mean(tribe2) 
bys tehs_code: egen p_tribe3 = mean(tribe3) 
bys tehs_code: egen p_tribe4 = mean(tribe4) 
bys tehs_code: egen p_tribe5 = mean(tribe5) 
bys tehs_code: egen p_tribe6 = mean(tribe6) 
bys tehs_code: egen p_tribe7 = mean(tribe7) 
bys tehs_code: egen p_tribe8 = mean(tribe8) 
bys tehs_code: egen p_tribe9 = mean(tribe9) 
bys tehs_code: egen p_tribe10 = mean(tribe10)
bys tehs_code: egen p_tribe11 = mean(tribe11)
bys tehs_code: egen p_tribe12 = mean(tribe12)
bys tehs_code: egen p_tribe13 = mean(tribe13)
bys tehs_code: egen p_tribe14 = mean(tribe14)
bys tehs_code: egen p_tribe15 = mean(tribe15)
bys tehs_code: egen p_tribe16 = mean(tribe16)
bys tehs_code: egen p_tribe17 = mean(tribe17)
bys tehs_code: egen p_tribe18 = mean(tribe18)
bys tehs_code: egen p_tribe19 = mean(tribe19)
bys tehs_code: egen p_tribe20 = mean(tribe20)
bys tehs_code: egen p_tribe21 = mean(tribe21)
bys tehs_code: egen p_tribe22 = mean(tribe22)
bys tehs_code: egen p_tribe23 = mean(tribe23)
bys tehs_code: egen p_tribe24 = mean(tribe24)
bys tehs_code: egen p_tribe25 = mean(tribe25)
bys tehs_code: egen p_tribe26 = mean(tribe26)
bys tehs_code: egen p_tribe27 = mean(tribe27)
bys tehs_code: egen p_tribe28 = mean(tribe28)
bys tehs_code: egen p_tribe29 = mean(tribe29)
bys tehs_code: egen p_tribe30 = mean(tribe30)
bys tehs_code: egen p_tribe31 = mean(tribe31)
bys tehs_code: egen p_tribe32 = mean(tribe32)
bys tehs_code: egen p_tribe33 = mean(tribe33)
bys tehs_code: egen p_tribe34 = mean(tribe34)
bys tehs_code: egen p_tribe35 = mean(tribe35)
bys tehs_code: egen p_tribe36 = mean(tribe36)
bys tehs_code: egen p_tribe37 = mean(tribe37)
bys tehs_code: egen p_tribe38 = mean(tribe38)
bys tehs_code: egen p_tribe39 = mean(tribe39)
bys tehs_code: egen p_tribe40 = mean(tribe40)
bys tehs_code: egen p_tribe41 = mean(tribe41)
bys tehs_code: egen p_tribe42 = mean(tribe42)
bys tehs_code: egen p_tribe43 = mean(tribe43)
bys tehs_code: egen p_tribe44 = mean(tribe44)
bys tehs_code: egen p_tribe45 = mean(tribe45)
bys tehs_code: egen p_tribe46 = mean(tribe46)
bys tehs_code: egen p_tribe47 = mean(tribe47)
bys tehs_code: egen p_tribe48 = mean(tribe48)
bys tehs_code: egen p_tribe49 = mean(tribe49)
bys tehs_code: egen p_tribe50 = mean(tribe50)
bys tehs_code: egen p_tribe51 = mean(tribe51)
bys tehs_code: egen p_tribe52 = mean(tribe52)
bys tehs_code: egen p_tribe53 = mean(tribe53)
bys tehs_code: egen p_tribe54 = mean(tribe54)
bys tehs_code: egen p_tribe55 = mean(tribe55)
bys tehs_code: egen p_tribe56 = mean(tribe56)
bys tehs_code: egen p_tribe57 = mean(tribe57)
bys tehs_code: egen p_tribe58 = mean(tribe58)
bys tehs_code: egen p_tribe59 = mean(tribe59)
bys tehs_code: egen p_tribe60 = mean(tribe60)
bys tehs_code: egen p_tribe61 = mean(tribe61)
bys tehs_code: egen p_tribe62 = mean(tribe62)
bys tehs_code: egen p_tribe63 = mean(tribe63)
bys tehs_code: egen p_tribe64 = mean(tribe64)
bys tehs_code: egen p_tribe65 = mean(tribe65)
bys tehs_code: egen p_tribe66 = mean(tribe66)
bys tehs_code: egen p_tribe67 = mean(tribe67)
bys tehs_code: egen p_tribe68 = mean(tribe68)
bys tehs_code: egen p_tribe69 = mean(tribe69)
bys tehs_code: egen p_tribe70 = mean(tribe70)
bys tehs_code: egen p_tribe71 = mean(tribe71)
bys tehs_code: egen p_tribe72 = mean(tribe72)
bys tehs_code: egen p_tribe73 = mean(tribe73)
bys tehs_code: egen p_tribe74 = mean(tribe74)
bys tehs_code: egen p_tribe75 = mean(tribe75)
bys tehs_code: egen p_tribe76 = mean(tribe76)
bys tehs_code: egen p_tribe77 = mean(tribe77)
bys tehs_code: egen p_tribe78 = mean(tribe78)
bys tehs_code: egen p_tribe79 = mean(tribe79)
bys tehs_code: egen p_tribe80 = mean(tribe80)
bys tehs_code: egen p_tribe81 = mean(tribe81)
bys tehs_code: egen p_tribe82 = mean(tribe82)
bys tehs_code: egen p_tribe83 = mean(tribe83)
bys tehs_code: egen p_tribe84 = mean(tribe84)
bys tehs_code: egen p_tribe85 = mean(tribe85)
bys tehs_code: egen p_tribe86 = mean(tribe86)
bys tehs_code: egen p_tribe87 = mean(tribe87)


bys tehs_code: gen biggest_tribe_tehs = max(p_tribe1, p_tribe2, p_tribe3, p_tribe4, p_tribe5, p_tribe6, p_tribe7, p_tribe8, p_tribe9, p_tribe10, p_tribe11, p_tribe12, p_tribe13, p_tribe14, p_tribe15, p_tribe16, p_tribe17, p_tribe18, p_tribe19, p_tribe20, p_tribe21, p_tribe22, p_tribe23, p_tribe24, p_tribe25, p_tribe26, p_tribe27, p_tribe28, p_tribe29, p_tribe30, p_tribe31, p_tribe32, p_tribe33, p_tribe34, p_tribe35, p_tribe36, p_tribe37, p_tribe38, p_tribe39, p_tribe40, p_tribe41, p_tribe42, p_tribe43, p_tribe44, p_tribe45, p_tribe46, p_tribe47, p_tribe48, p_tribe49, p_tribe50, p_tribe51, p_tribe52, p_tribe53, p_tribe54, p_tribe55, p_tribe56, p_tribe57, p_tribe58, p_tribe59, p_tribe60, p_tribe61, p_tribe62, p_tribe63, p_tribe64, p_tribe65, p_tribe66, p_tribe67, p_tribe68, p_tribe69, p_tribe70, p_tribe71, p_tribe72, p_tribe73, p_tribe74, p_tribe75, p_tribe76, p_tribe77, p_tribe78, p_tribe79, p_tribe80, p_tribe81, p_tribe82, p_tribe83, p_tribe84, p_tribe85, p_tribe86, p_tribe87)

gen max_tribe_tehs=1 if p_tribe1==biggest_tribe_tehs 
replace max_tribe_tehs=2 if p_tribe2==biggest_tribe_tehs 
replace max_tribe_tehs=3 if p_tribe3==biggest_tribe_tehs 
replace max_tribe_tehs=4 if p_tribe4==biggest_tribe_tehs 
replace max_tribe_tehs=5 if p_tribe5==biggest_tribe_tehs 
replace max_tribe_tehs=6 if p_tribe6==biggest_tribe_tehs 
replace max_tribe_tehs=7 if p_tribe7==biggest_tribe_tehs 
replace max_tribe_tehs=8 if p_tribe8==biggest_tribe_tehs 
replace max_tribe_tehs=9 if p_tribe9==biggest_tribe_tehs 
replace max_tribe_tehs=10 if p_tribe10==biggest_tribe_tehs 
replace max_tribe_tehs=11 if p_tribe11==biggest_tribe_tehs 
replace max_tribe_tehs=12 if p_tribe12==biggest_tribe_tehs 
replace max_tribe_tehs=13 if p_tribe13==biggest_tribe_tehs 
replace max_tribe_tehs=14 if p_tribe14==biggest_tribe_tehs 
replace max_tribe_tehs=15 if p_tribe15==biggest_tribe_tehs 
replace max_tribe_tehs=16 if p_tribe16==biggest_tribe_tehs 
replace max_tribe_tehs=17 if p_tribe17==biggest_tribe_tehs 
replace max_tribe_tehs=18 if p_tribe18==biggest_tribe_tehs 
replace max_tribe_tehs=19 if p_tribe19==biggest_tribe_tehs 
replace max_tribe_tehs=20 if p_tribe20==biggest_tribe_tehs 
replace max_tribe_tehs=21 if p_tribe21==biggest_tribe_tehs 
replace max_tribe_tehs=22 if p_tribe22==biggest_tribe_tehs 
replace max_tribe_tehs=23 if p_tribe23==biggest_tribe_tehs 
replace max_tribe_tehs=24 if p_tribe24==biggest_tribe_tehs 
replace max_tribe_tehs=25 if p_tribe25==biggest_tribe_tehs 
replace max_tribe_tehs=26 if p_tribe26==biggest_tribe_tehs 
replace max_tribe_tehs=27 if p_tribe27==biggest_tribe_tehs 
replace max_tribe_tehs=28 if p_tribe28==biggest_tribe_tehs 
replace max_tribe_tehs=29 if p_tribe29==biggest_tribe_tehs 
replace max_tribe_tehs=30 if p_tribe30==biggest_tribe_tehs 
replace max_tribe_tehs=31 if p_tribe31==biggest_tribe_tehs 
replace max_tribe_tehs=32 if p_tribe32==biggest_tribe_tehs 
replace max_tribe_tehs=33 if p_tribe33==biggest_tribe_tehs 
replace max_tribe_tehs=34 if p_tribe34==biggest_tribe_tehs 
replace max_tribe_tehs=35 if p_tribe35==biggest_tribe_tehs 
replace max_tribe_tehs=36 if p_tribe36==biggest_tribe_tehs 
replace max_tribe_tehs=37 if p_tribe37==biggest_tribe_tehs 
replace max_tribe_tehs=38 if p_tribe38==biggest_tribe_tehs 
replace max_tribe_tehs=39 if p_tribe39==biggest_tribe_tehs 
replace max_tribe_tehs=40 if p_tribe40==biggest_tribe_tehs 
replace max_tribe_tehs=41 if p_tribe41==biggest_tribe_tehs 
replace max_tribe_tehs=42 if p_tribe42==biggest_tribe_tehs 
replace max_tribe_tehs=43 if p_tribe43==biggest_tribe_tehs 
replace max_tribe_tehs=44 if p_tribe44==biggest_tribe_tehs 
replace max_tribe_tehs=45 if p_tribe45==biggest_tribe_tehs 
replace max_tribe_tehs=46 if p_tribe46==biggest_tribe_tehs 
replace max_tribe_tehs=47 if p_tribe47==biggest_tribe_tehs 
replace max_tribe_tehs=48 if p_tribe48==biggest_tribe_tehs 
replace max_tribe_tehs=49 if p_tribe49==biggest_tribe_tehs 
replace max_tribe_tehs=50 if p_tribe50==biggest_tribe_tehs 
replace max_tribe_tehs=51 if p_tribe51==biggest_tribe_tehs 
replace max_tribe_tehs=52 if p_tribe52==biggest_tribe_tehs 
replace max_tribe_tehs=53 if p_tribe53==biggest_tribe_tehs 
replace max_tribe_tehs=54 if p_tribe54==biggest_tribe_tehs 
replace max_tribe_tehs=55 if p_tribe55==biggest_tribe_tehs 
replace max_tribe_tehs=56 if p_tribe56==biggest_tribe_tehs 
replace max_tribe_tehs=57 if p_tribe57==biggest_tribe_tehs 
replace max_tribe_tehs=58 if p_tribe58==biggest_tribe_tehs 
replace max_tribe_tehs=59 if p_tribe59==biggest_tribe_tehs 
replace max_tribe_tehs=60 if p_tribe60==biggest_tribe_tehs 
replace max_tribe_tehs=61 if p_tribe61==biggest_tribe_tehs 
replace max_tribe_tehs=62 if p_tribe62==biggest_tribe_tehs 
replace max_tribe_tehs=63 if p_tribe63==biggest_tribe_tehs 
replace max_tribe_tehs=64 if p_tribe64==biggest_tribe_tehs 
replace max_tribe_tehs=65 if p_tribe65==biggest_tribe_tehs 
replace max_tribe_tehs=66 if p_tribe66==biggest_tribe_tehs 
replace max_tribe_tehs=67 if p_tribe67==biggest_tribe_tehs 
replace max_tribe_tehs=68 if p_tribe68==biggest_tribe_tehs 
replace max_tribe_tehs=69 if p_tribe69==biggest_tribe_tehs 
replace max_tribe_tehs=70 if p_tribe70==biggest_tribe_tehs 
replace max_tribe_tehs=71 if p_tribe71==biggest_tribe_tehs 
replace max_tribe_tehs=72 if p_tribe72==biggest_tribe_tehs 
replace max_tribe_tehs=73 if p_tribe73==biggest_tribe_tehs 
replace max_tribe_tehs=74 if p_tribe74==biggest_tribe_tehs 
replace max_tribe_tehs=75 if p_tribe75==biggest_tribe_tehs 
replace max_tribe_tehs=76 if p_tribe76==biggest_tribe_tehs 
replace max_tribe_tehs=77 if p_tribe77==biggest_tribe_tehs 
replace max_tribe_tehs=78 if p_tribe78==biggest_tribe_tehs 
replace max_tribe_tehs=79 if p_tribe79==biggest_tribe_tehs 
replace max_tribe_tehs=80 if p_tribe80==biggest_tribe_tehs 
replace max_tribe_tehs=81 if p_tribe81==biggest_tribe_tehs 
replace max_tribe_tehs=82 if p_tribe82==biggest_tribe_tehs 
replace max_tribe_tehs=83 if p_tribe83==biggest_tribe_tehs 
replace max_tribe_tehs=84 if p_tribe84==biggest_tribe_tehs 
replace max_tribe_tehs=85 if p_tribe85==biggest_tribe_tehs 
replace max_tribe_tehs=86 if p_tribe86==biggest_tribe_tehs 
replace max_tribe_tehs=87 if p_tribe87==biggest_tribe_tehs 



gen minority_tehs = 1 
replace minority_tehs = 0 if max_tribe_tehs==tribe_code2 

foreach var of varlist p_tribe1 p_tribe2 p_tribe3 p_tribe4 p_tribe5 p_tribe6 p_tribe7 p_tribe8 p_tribe9 p_tribe10 p_tribe11 p_tribe12 p_tribe13 p_tribe14 p_tribe15 p_tribe16 p_tribe17 p_tribe18 p_tribe19 p_tribe20 p_tribe21 p_tribe22 p_tribe23 p_tribe24 p_tribe25 p_tribe26 p_tribe27 p_tribe28 p_tribe29 p_tribe30 p_tribe31 p_tribe32 p_tribe33 p_tribe34 p_tribe35 p_tribe36 p_tribe37 p_tribe38 p_tribe39 p_tribe40 p_tribe41 p_tribe42 p_tribe43 p_tribe44 p_tribe45 p_tribe46 p_tribe47 p_tribe48 p_tribe49 p_tribe50 p_tribe51 p_tribe52 p_tribe53 p_tribe54 p_tribe55 p_tribe56 p_tribe57 p_tribe58 p_tribe59 p_tribe60 p_tribe61 p_tribe62 p_tribe63 p_tribe64 p_tribe65 p_tribe66 p_tribe67 p_tribe68 p_tribe69 p_tribe70 p_tribe71 p_tribe72 p_tribe73 p_tribe74 p_tribe75 p_tribe76 p_tribe77 p_tribe78 p_tribe79 p_tribe80 p_tribe81 p_tribe82 p_tribe83 p_tribe84 p_tribe85 p_tribe86 p_tribe87{
gen p`var' = `var'*100
}

* Fractionalization index (TEHSIL)
foreach var of varlist pp_tribe1 pp_tribe2 pp_tribe3 pp_tribe4 pp_tribe5 pp_tribe6 pp_tribe7 pp_tribe8 pp_tribe9 pp_tribe10 pp_tribe11 pp_tribe12 pp_tribe13 pp_tribe14 pp_tribe15 pp_tribe16 pp_tribe17 pp_tribe18 pp_tribe19 pp_tribe20 pp_tribe21 pp_tribe22 pp_tribe23 pp_tribe24 pp_tribe25 pp_tribe26 pp_tribe27 pp_tribe28 pp_tribe29 pp_tribe30 pp_tribe31 pp_tribe32 pp_tribe33 pp_tribe34 pp_tribe35 pp_tribe36 pp_tribe37 pp_tribe38 pp_tribe39 pp_tribe40 pp_tribe41 pp_tribe42 pp_tribe43 pp_tribe44 pp_tribe45 pp_tribe46 pp_tribe47 pp_tribe48 pp_tribe49 pp_tribe50 pp_tribe51 pp_tribe52 pp_tribe53 pp_tribe54 pp_tribe55 pp_tribe56 pp_tribe57 pp_tribe58 pp_tribe59 pp_tribe60 pp_tribe61 pp_tribe62 pp_tribe63 pp_tribe64 pp_tribe65 pp_tribe66 pp_tribe67 pp_tribe68 pp_tribe69 pp_tribe70 pp_tribe71 pp_tribe72 pp_tribe73 pp_tribe74 pp_tribe75 pp_tribe76 pp_tribe77 pp_tribe78 pp_tribe79 pp_tribe80 pp_tribe81 pp_tribe82 pp_tribe83 pp_tribe84 pp_tribe85 pp_tribe86 pp_tribe87{
gen one_minus_`var' = 100-`var'
gen multipl_`var' = `var'*one_minus_`var'
}
egen temp_tribal_frac=rowtotal(multipl_*), missing
gen tribal_frac1 = temp_tribal_frac/10000
drop temp_* one_minus* multipl_*
label variable tribal_frac1 "Tribal fractionalization (tehsil)"


drop p_tribe1 p_tribe2 p_tribe3 p_tribe4 p_tribe5 p_tribe6 p_tribe7 p_tribe8 p_tribe9 p_tribe10 p_tribe11 p_tribe12 p_tribe13 p_tribe14 p_tribe15 p_tribe16 p_tribe17 p_tribe18 p_tribe19 p_tribe20 p_tribe21 p_tribe22 p_tribe23 p_tribe24 p_tribe25 p_tribe26 p_tribe27 p_tribe28 p_tribe29 p_tribe30 p_tribe31 p_tribe32 p_tribe33 p_tribe34 p_tribe35 p_tribe36 p_tribe37 p_tribe38 p_tribe39 p_tribe40 p_tribe41 p_tribe42 p_tribe43 p_tribe44 p_tribe45 p_tribe46 p_tribe47 p_tribe48 p_tribe49 p_tribe50 p_tribe51 p_tribe52 p_tribe53 p_tribe54 p_tribe55 p_tribe56 p_tribe57 p_tribe58 p_tribe59 p_tribe60 p_tribe61 p_tribe62 p_tribe63 p_tribe64 p_tribe65 p_tribe66 p_tribe67 p_tribe68 p_tribe69 p_tribe70 p_tribe71 p_tribe72 p_tribe73 p_tribe74 p_tribe75 p_tribe76 p_tribe77 p_tribe78 p_tribe79 p_tribe80 p_tribe81 p_tribe82 p_tribe83 p_tribe84 p_tribe85 p_tribe86 p_tribe87 pp_tribe1 pp_tribe2 pp_tribe3 pp_tribe4 pp_tribe5 pp_tribe6 pp_tribe7 pp_tribe8 pp_tribe9 pp_tribe10 pp_tribe11 pp_tribe12 pp_tribe13 pp_tribe14 pp_tribe15 pp_tribe16 pp_tribe17 pp_tribe18 pp_tribe19 pp_tribe20 pp_tribe21 pp_tribe22 pp_tribe23 pp_tribe24 pp_tribe25 pp_tribe26 pp_tribe27 pp_tribe28 pp_tribe29 pp_tribe30 pp_tribe31 pp_tribe32 pp_tribe33 pp_tribe34 pp_tribe35 pp_tribe36 pp_tribe37 pp_tribe38 pp_tribe39 pp_tribe40 pp_tribe41 pp_tribe42 pp_tribe43 pp_tribe44 pp_tribe45 pp_tribe46 pp_tribe47 pp_tribe48 pp_tribe49 pp_tribe50 pp_tribe51 pp_tribe52 pp_tribe53 pp_tribe54 pp_tribe55 pp_tribe56 pp_tribe57 pp_tribe58 pp_tribe59 pp_tribe60 pp_tribe61 pp_tribe62 pp_tribe63 pp_tribe64 pp_tribe65 pp_tribe66 pp_tribe67 pp_tribe68 pp_tribe69 pp_tribe70 pp_tribe71 pp_tribe72 pp_tribe73 pp_tribe74 pp_tribe75 pp_tribe76 pp_tribe77 pp_tribe78 pp_tribe79 pp_tribe80 pp_tribe81 pp_tribe82 pp_tribe83 pp_tribe84 pp_tribe85 pp_tribe86 pp_tribe87

bys tehs_code: gen tehs=_n


drop tribe1 tribe2 tribe3 tribe4 tribe5 tribe6 tribe7 tribe8 tribe9 tribe10 tribe11 tribe12 tribe13 tribe14 tribe15 tribe16 tribe17 tribe18 tribe19 tribe20 tribe21 tribe22 tribe23 tribe24 tribe25 tribe26 tribe27 tribe28 tribe29 tribe30 tribe31 tribe32 tribe33 tribe34 tribe35 tribe36 tribe37 tribe38 tribe39 tribe40 tribe41 tribe42 tribe43 tribe44 tribe45 tribe46 tribe47 tribe48 tribe49 tribe50 tribe51 tribe52 tribe53 tribe54 tribe55 tribe56 tribe57 tribe58 tribe59 tribe60 tribe61 tribe62 tribe63 tribe64 tribe65 tribe66 tribe67 tribe68 tribe69 tribe70 tribe71 tribe72 tribe73 tribe74 tribe75 tribe76 tribe77 tribe78 tribe79 tribe80 tribe81 tribe82 tribe83 tribe84 tribe85 tribe86 tribe87




******************************
****       OUTCOMES       ****
******************************


recode taxes 1=4 2=3 3=2 4=1 5=0 6=., gen(taxes1)
recode formal_justice 1=4 2=3 3=2 4=1 5=0 6=., gen(formal_justice1)
recode policing 1=4 2=3 3=2 4=1 5=0 6=., gen(policing1)



zval tribal_frac0 tribal_frac1 taxes1 formal_justice1 policing1  
label variable z_tribal_frac0 "Tribal fractionalization (village)"

egen enforcement1 = rowmean(formal_justice1 policing1)
zval enforcement1 


save "${PathData}fata_data_max_recoded.dta", replace

